#!/usr/bin/env node

const http = require('http'),
      fs = require('fs');
const data = require('./data.js');

http.createServer((req, res) => {
  if (req.url === '/') {
    let html = fs.readFileSync('./index.html').toString('utf8');

    res.setHeader('Content-Type', 'text/html');

    res.end(html);
  } else if (req.url === '/getlist') {
    let movie = data.filter(item => item.movieRange !== '暂无评分');
     
    movie.sort((a, b) => {
      return b.movieRange - a.movieRange
    })

    res.setHeader('Content-Type', 'text/json');
    res.end(JSON.stringify(movie));
  } else {
    err(res);
  }
}).listen(8080)

function err(res) {
  let msg = 'Not Found!';
  res.statusCode = 404;
  res.setHeader('Content-Type', 'text/plain');
  res.setHeader('Content-Length', msg.length);
  res.end(msg);
}      
